import requests

from config import BASE_URL


class CourseAPI:
    def __init__(self):
        self.add_course_url = BASE_URL + "/api/clues/course/"
        self.query_course_url = BASE_URL + "/api/clues/course/list"
        self.amend_course_url = BASE_URL + "/api/clues/course"
        self.delete_course_url = BASE_URL + "/api/clues/course/{}"

    def add_course(self, token, name, subject="6", price=899, applicablePerson="2", info=""):
        headers = {"Authorization": token}
        add_course_data = {
            "name": name,
            "subject": subject,
            "price": price,
            "applicablePerson": applicablePerson,
            "info": info
        }

        response = requests.post(self.add_course_url, json=add_course_data, headers=headers)
        return response

    def query_course(self, token, name="", subject="", price=None, applicablePerson="", info=""):
        headers = {"Authorization": token}
        query_data = {
            "name": name,
            "subject": subject,
            "price": price,
            "applicablePerson": applicablePerson,
            "info": info
        }

        response = requests.get(self.query_course_url, params=query_data, headers=headers)
        return response

    def amend_course(self, token, id, name="", subject="", price=None, applicablePerson="", info=""):
        headers = {"Authorization": token}
        amend_data = {
            "id": id,
            "name": name,
            "subject": subject,
            "price": price,
            "applicablePerson": applicablePerson,
            "info": info
        }

        response = requests.put(self.amend_course_url, json=amend_data, headers=headers)
        return response

    def delets_course(self, token, id):
        headers = {"Authorization": token}
        result_url = self.delete_course_url.format(id)

        response = requests.delete(result_url, headers=headers)
        return response
